﻿using HandleSign.DataBase.Base;
using HandleSign.Models;
using ServerPA.CompletPort.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;

namespace HandleSign.DataBase.AfterClass
{
    public class SystemDataSet
    {

        /// <summary>
        /// //查询设置参数
        /// </summary>
        public static List<SystemDataSetModel> GetSet(string connstr)//查询设置参数
        {
            List<SystemDataSetModel> sdsModels = new List<SystemDataSetModel>();
            DataBaseDao DBdao = new DataBaseDao(connstr);

            string sSet = @"SELECT [set_StartEvenNum],[set_TimeThresHold],[set_TerminalCheNum],[set_TerminalStuChe],[set_TimeSetS],[set_TimeSetE],[set_SgroupId] FROM [PhoneSign].[dbo].[P_Set] ";

            DataTable dt_Set = DBdao.Query_dt(sSet);
            

            try
            {
                foreach (DataRow dr in dt_Set.Rows)
                {
                    SystemDataSetModel sdsModel = new SystemDataSetModel();
                    sdsModel.GroupId = dr["set_SgroupId"].ToString();//GroupId
                    sdsModel.tercount = Convert.ToInt32(dr["Set_TerminalStuChe"]);//获取前几个终端数
                    sdsModel.holdtime = Convert.ToInt32(dr["set_TimeThresHold"]);//时间阈值
                    sdsModel.usecoutn = Convert.ToInt32(dr["set_StartEvenNum"]);//开始事件达到的人数
                    sdsModel.chenum = Convert.ToInt32(dr["set_TerminalCheNum"]);//有效的签到终端个数
                    sdsModel.TimeSetS = Convert.ToInt32(dr["set_TimeSetS"]);//上课提前时间
                    sdsModel.TimeSetE = Convert.ToInt32(dr["set_TimeSetE"]);//下课延后时间
                    sdsModels.Add(sdsModel);
                }
            }
            catch (Exception ex)
            {

                DBdao.Dispose();
            }


            DBdao.Dispose();
            return sdsModels;
        }


        /// <summary>
        /// //查询设置参数
        /// </summary>
        public static List<TimeInfo> GetTime(string connstr)//查询设置参数
        {

            DataBaseDao DBdao = new DataBaseDao(connstr);

            string sTime = @"SELECT  [time_num],[time_SgroupId],[time_Stime],[time_Etime],[time_Compus],time_SgroupId FROM[AccessDB].[dbo].[R_Times]   where time_view = 1";

            DataTable dt_Time = DBdao.Query_dt(sTime);


            List<TimeInfo> timeList = new List<TimeInfo>();

            try
            {


                foreach (DataRow dr_Time in dt_Time.Rows)
                {
                    TimeInfo tiModel = new TimeInfo();
                    tiModel.timeNum = Convert.ToInt32(dr_Time["time_num"]);

                    tiModel.groupId = dr_Time["time_SgroupId"].ToString();
                    tiModel.etime = dr_Time["time_Etime"].ToString();
                    tiModel.stime = dr_Time["time_Stime"].ToString();

                    timeList.Add(tiModel);
                }

            }
            catch (Exception ex)
            {
                LogUtil.LogToFile(ex, "SystemDataSet_Exception");
            }

            DBdao.Dispose();
            return timeList;
        }




        /// <summary>
        /// 返回基础信息
        /// </summary>
        /// <param name="connstr"></param>
        /// <returns></returns>
        public static BaseInfo GetBaseInfo(string connstr)
        {

            DataBaseDao DBdao = new DataBaseDao(connstr);
            BaseInfo baseInfo = new BaseInfo();
            //查询所在教室
            string roomsql = "select [At_RoomId],[At_TerminalId],At_Sgroupid from [PhoneSign].[dbo].[P_AgentTerminal] where At_RoomId  != 0";
            // string Classsql = "select [task_classid],[Stb_ScanTaskNum] from [AccessDB].[dbo].[TaskBindNum]";

            //  DataTable dt_Class = DBdao.Query_dt(Classsql);
            DataTable dt_Room = DBdao.Query_dt(roomsql);


            //List<ClassInfo> classList = new List<ClassInfo>();
            List<TerminalInfo> terminalList = new List<TerminalInfo>();

            //foreach (DataRow dr_Class in dt_Class.Rows)
            //{
            //    ClassInfo classInfo = new ClassInfo();

            //    classInfo.classId2task = Convert.ToInt32(dr_Class["task_classid"]);
            //    classInfo.scanTaskNum = dr_Class["Stb_ScanTaskNum"].ToString();
            //    classList.Add(classInfo);

            //}

            foreach (DataRow dr_Room in dt_Room.Rows)
            {
                TerminalInfo terminalInfo = new TerminalInfo();

                terminalInfo.roomId = Convert.ToInt32(dr_Room["At_RoomId"]);
                terminalInfo.terminalId = dr_Room["At_TerminalId"].ToString();
                terminalList.Add(terminalInfo);
            }


            // baseInfo.classList = classList;
            baseInfo.terminalList = terminalList;

            DBdao.Dispose();
            return baseInfo;
        }


    }
}